<?php
class main extends top
{
    function __construct(){  
        parent::__construct();
    }
    function index(){
       $this->display("main_index.html");
    }
    function login(){
        $data=$this->spArgs();

        
        if (empty($data['username']) || empty($data['pwd']))
            $this->__error("danger","用户名或密码不能为空!");

        $user = spClass('m_user');
        $conditions = array('username' =>$data['username']);
        $login_result = $user->find($conditions);

        if ($login_result) {
            if ($login_result['pwd'] == $data['pwd']) {
                //将用户数据存到session里面。
                $this->login_do($login_result);
                $this->info();
            }else{
                $this->__error("danger","密码错误!");
            }
        } else {
            $this->__error("danger","用户名不存在!");
        }        
    }
    function login_do($login_result) {
        $_SESSION['id'] = $login_result['id'];
        $_SESSION['username'] = $login_result['username'];
        $_SESSION['password'] =$login_result['pwd'];
    }
    function info(){
        $user_info = $_SESSION;

        echo json_encode($user_info);
    }
    function is_login(){
        if($this->check_session())
            $this->__error("success","已登录!"); 
        else
            $this->__error("danger","未登录!"); 
    }
    function logout() {
        session_destroy();
    }
}